<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/15
 * Time: 14:56
 */

class Role extends ADM_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->view(parent::TMP_DIR . '/admin/role.html');
    }

    public function add()
    {
        $this->load->view(parent::TMP_DIR . '/admin/add_role.html');
    }

    public function getRoles()
    {

        if ($this->role == 'base' && !empty($this->baseId)) {
            $roles = [[
                'id' => 0,
                'name' => '供应商管理员'
            ]];
        } else {
            $q = $this->db->get('admin_role');
            $rows = $q->result_array();
            $roles = [];
            foreach ($rows as $item) {
                if ($item['id'] == 1) {
                    continue;
                }
                $roles[] = [
                    'id' => $item['id'],
                    'name' => $item['role']
                ];
            }
        }

        $this->output->send([
            'state' => true,
            'data' => $roles
        ]);
    }

    public function roles()
    {
        $this->output->send([
            'state' => true,
            'data' => $this->getAllMenus()
        ]);
    }

    public function remove()
    {
        $send['state'] = false;
        do {

            $roleId = $this->input->post('roleId');
            if (empty($roleId)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('common/Admin_Model', 'admin');
            if ($this->admin->remove($roleId)) {
                $send['state'] = true;
            } else {
                $send['error'] = '删除失败';
            }

        } while(false);
        $this->output->send($send);
    }

    /**
     * 检查
     */
    public function check()
    {
        $send['state'] = false;
        do {

            $roleId = $this->input->get('roleId');
            if (empty($roleId)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('common/Admin_Model', 'admin');
            $users = $this->admin->check($roleId);
            if (!empty($users) || count($users) > 0) {
                $send['error'] = '包含用户';
                break;
            }

            $send['state'] = true;

        } while(false);
        $this->output->send($send);
    }


    /**
     * 修改
     */
    public function edit()
    {
        $send['state'] = false;
        do {
            $roleId = $this->input->post('roleId');
            $roleName = $this->input->post('name');
            $roles = $this->input->post('role');
            if (empty($roleId)) {
                $send['error'] = '参数错误';
                break;
            }

            if (empty($roleName)) {
                $send['error'] = '请填写角色名称';
                break;
            }
            if (empty($roles)) {
                $send['error'] = '请选择用户访问权限';
                break;
            }

            $this->load->model('common/Role_Model', 'r');
            $res = $this->r->edit($roleId, $roleName, $roles);
            if ($res) {
                $send['state'] = true;
                break;
            }

            $send['error'] = '用户角色编辑失败';

        } while(false);
        $this->output->send($send);
    }

    /**
     * 保存
     */
    public function save()
    {
        $send['state'] = false;
        do {

            $roleName = $this->input->post('name');
            $roles = $this->input->post('role');
            if (empty($roleName)) {
                $send['error'] = '请填写角色名称';
                break;
            }
            if (empty($roles)) {
                $send['error'] = '请选择用户访问权限';
                break;
            }

            $this->load->model('common/Role_Model', 'r');
            $res = $this->r->save($roleName, $roles);
            if ($res) {
                $send['state'] = true;
                break;
            }

            $send['error'] = '用户角色创建失败';

        } while(false);
        $this->output->send($send);
    }

    /**
     * 获取
     */
    public function get()
    {
        $send['state'] = false;
        do {
            $this->load->model('common/Role_Model', 'r');
            $rows = $this->r->getRoles();
            $send['state'] = true;
            $send['data'] = $rows;
        } while(false);
        $this->output->send($send);
    }

    /**
     * 获取一条
     */
    public function getItem()
    {
        $send['state'] = false;
        do {

            $roleId = $this->input->get('roleId');
            if (empty($roleId)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('common/Role_Model', 'r');
            $val = $this->r->getItem($roleId);

            if ($val) {
                $send['state'] = true;
                $send['data'] = ['name' => $val['role'], 'value' => json_decode($val['value'], true)];
                break;
            }

            $send['error'] = '未知错误';

        } while(false);
        $this->output->send($send);
    }
}
















